#include <gl/GL.h>
#include <gl/GLU.h>
#include "GameRenderer.h"
#include "GameData.h"
#include "TerrainRenderer.h"
#include "Camera.h"
#include "SceneManager.h"

GameRenderer::GameRenderer(GameData* data) {
    _data = data;
    _terrain_renderer = new TerrainRenderer(data);
}

GameRenderer::~GameRenderer() {
    delete _terrain_renderer;
}

TerrainRenderer* GameRenderer::terrain_renderer() {
    return _terrain_renderer;
}

void GameRenderer::init() {

    glEnable(GL_DEPTH_TEST);
    glEnable(GL_COLOR_MATERIAL);

    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glShadeModel(GL_SMOOTH);
    glEnable(GL_CULL_FACE);
    GLfloat lpos0[4] = {0.0f, 100.0f, 100.0f, 0.0f};
    glLightfv(GL_LIGHT0, GL_POSITION, lpos0);

    //glMaterialf(GL_FRONT, GL_AMBIENT, (0.1f, 0.1f, 0.1f, 1.0f));
    //glMaterialf(GL_FRONT, GL_DIFFUSE, (1.0f, 1.0f, 1.0f, 1.0f));

    glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
}

void GameRenderer::resize(int width, int height) {
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60.0, (float) width / height, 0.1, 10000.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void GameRenderer::draw() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    _data->camera()->apply_camera_transform();
    glPushMatrix();
    GLfloat lpos0[4] = {500.0f, 500.0f, 500.0f, 0.0f};
    glLightfv(GL_LIGHT0, GL_POSITION, lpos0);
    //glTranslatef(0.0f, 0.0f, -10.0f);
    //glColor3f(0.2f, 0.8f, 0.3f);

    //float zero = -5.0f;
    //float one = 5.0f;

//    glBegin(GL_QUADS);
//    glVertex3f(zero, zero, zero);
//    glVertex3f(zero, one, zero);
//    glVertex3f(one, one, zero);
//    glVertex3f(one, zero, zero);
//
//    glVertex3f(zero, zero, zero);
//    glVertex3f(zero, zero, one);
//    glVertex3f(zero, one, one);
//    glVertex3f(zero, one, zero);
//
//    glVertex3f(zero, zero, one);
//    glVertex3f(one, zero, one);
//    glVertex3f(one, one, one);
//    glVertex3f(zero, one, one);
//
//    glVertex3f(one, zero, one);
//    glVertex3f(one, zero, zero);
//    glVertex3f(one, one, zero);
//    glVertex3f(one, one, one);
//
//    glVertex3f(zero, one, zero);
//    glVertex3f(zero, one, one);
//    glVertex3f(one, one, one);
//    glVertex3f(one, one, zero);
//
//    glEnd();
    glPopMatrix();
    _data->scene()->draw();
    _terrain_renderer->draw();

}